/*
 * DBCheckView.java
 */

package com.fernandomartines.dbcheck.app.view;

import com.fernandomartines.dbcheck.DBCheckApp;
import com.fernandomartines.dbcheck.app.controller.DBCheckController;
import java.awt.Image;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class DBCheckView extends FrameView {

    public DBCheckView(SingleFrameApplication app) {
        super(app);

        Image appIcon = new ImageIcon(getClass().getResource("/com/fernandomartines/dbcheck/app/view/resources/dbCheck-db-icon.png")).getImage();
        this.getFrame().setIconImage(appIcon);

        initComponents();

        new DBCheckController(this);

        this.menuBar.setVisible(true);

        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getStatusMessageLabel().setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                getStatusAnimationLabel().setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        getStatusAnimationLabel().setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    getProgressBar().setVisible(true);
                    getProgressBar().setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    getStatusAnimationLabel().setIcon(idleIcon);
                    getProgressBar().setVisible(false);
                    getProgressBar().setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    getStatusMessageLabel().setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    getProgressBar().setVisible(true);
                    getProgressBar().setIndeterminate(false);
                    getProgressBar().setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (getAboutBox() == null) {
            JFrame mainFrame = DBCheckApp.getApplication().getMainFrame();
            setAboutBox(new DBCheckAboutBox(mainFrame));
            getAboutBox().setLocationRelativeTo(mainFrame);
        }
        DBCheckApp.getApplication().show(getAboutBox());
    }

    @Action
    public void createNewTestSuite() {
    }

    @Action
    public void openExistingTestSuite() {
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        appPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        container = new javax.swing.JPanel();
        split = new javax.swing.JPanel();
        panelContainerSelected = new javax.swing.JPanel();
        panelSelectedTestSuite = new javax.swing.JPanel();
        panelContainerList = new javax.swing.JPanel();
        panelTestSuiteListContainer = new javax.swing.JPanel();
        labelTestSuites = new javax.swing.JLabel();
        panelTestSuite = new javax.swing.JPanel();
        scrollPane = new javax.swing.JScrollPane();
        panelTestSuiteList = new javax.swing.JPanel();
        buttonCreateTestSuite = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        menuItemNewTestSuite = new javax.swing.JMenuItem();
        menuSep1 = new javax.swing.JPopupMenu.Separator();
        menuItemExit = new javax.swing.JMenuItem();
        menuHelp = new javax.swing.JMenu();
        menuItemAbout = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setLayout(new java.awt.BorderLayout());

        appPanel.setBackground(new java.awt.Color(254, 254, 254));
        appPanel.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 0, 1, 0, new java.awt.Color(0, 0, 0)));
        appPanel.setPreferredSize(new java.awt.Dimension(400, 67));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fernandomartines/dbcheck/app/view/resources/dbCheck-logo.png"))); // NOI18N

        javax.swing.GroupLayout appPanelLayout = new javax.swing.GroupLayout(appPanel);
        appPanel.setLayout(appPanelLayout);
        appPanelLayout.setHorizontalGroup(
            appPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(appPanelLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        appPanelLayout.setVerticalGroup(
            appPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        mainPanel.add(appPanel, java.awt.BorderLayout.NORTH);

        container.setLayout(new java.awt.BorderLayout());

        split.setLayout(new java.awt.BorderLayout());

        panelContainerSelected.setLayout(new java.awt.BorderLayout());

        panelSelectedTestSuite.setLayout(new java.awt.BorderLayout());
        panelContainerSelected.add(panelSelectedTestSuite, java.awt.BorderLayout.CENTER);

        split.add(panelContainerSelected, java.awt.BorderLayout.CENTER);

        panelContainerList.setLayout(new java.awt.BorderLayout());

        panelTestSuiteListContainer.setBackground(new java.awt.Color(150, 150, 150));
        panelTestSuiteListContainer.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 0, 1, new java.awt.Color(0, 0, 0)));
        panelTestSuiteListContainer.setPreferredSize(new java.awt.Dimension(350, 300));

        labelTestSuites.setBackground(new java.awt.Color(75, 75, 75));
        labelTestSuites.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelTestSuites.setText(" Test Suites  ");
        labelTestSuites.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(50, 50, 50)));
        labelTestSuites.setOpaque(true);

        panelTestSuite.setLayout(new java.awt.BorderLayout());

        scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        panelTestSuiteList.setBackground(new java.awt.Color(210, 210, 210));

        javax.swing.GroupLayout panelTestSuiteListLayout = new javax.swing.GroupLayout(panelTestSuiteList);
        panelTestSuiteList.setLayout(panelTestSuiteListLayout);
        panelTestSuiteListLayout.setHorizontalGroup(
            panelTestSuiteListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 360, Short.MAX_VALUE)
        );
        panelTestSuiteListLayout.setVerticalGroup(
            panelTestSuiteListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 258, Short.MAX_VALUE)
        );

        scrollPane.setViewportView(panelTestSuiteList);

        panelTestSuite.add(scrollPane, java.awt.BorderLayout.CENTER);

        buttonCreateTestSuite.setText("Create Test Suite");

        javax.swing.GroupLayout panelTestSuiteListContainerLayout = new javax.swing.GroupLayout(panelTestSuiteListContainer);
        panelTestSuiteListContainer.setLayout(panelTestSuiteListContainerLayout);
        panelTestSuiteListContainerLayout.setHorizontalGroup(
            panelTestSuiteListContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTestSuiteListContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTestSuiteListContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelTestSuite, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addGroup(panelTestSuiteListContainerLayout.createSequentialGroup()
                        .addComponent(labelTestSuites, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCreateTestSuite)))
                .addContainerGap())
        );
        panelTestSuiteListContainerLayout.setVerticalGroup(
            panelTestSuiteListContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTestSuiteListContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTestSuiteListContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelTestSuites, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonCreateTestSuite, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(7, 7, 7)
                .addComponent(panelTestSuite, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelContainerList.add(panelTestSuiteListContainer, java.awt.BorderLayout.CENTER);

        split.add(panelContainerList, java.awt.BorderLayout.WEST);

        container.add(split, java.awt.BorderLayout.CENTER);

        mainPanel.add(container, java.awt.BorderLayout.CENTER);

        menuBar.setPreferredSize(new java.awt.Dimension(60, 25));

        menuFile.setText("File");

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.fernandomartines.dbcheck.DBCheckApp.class).getContext().getActionMap(DBCheckView.class, this);
        menuItemNewTestSuite.setAction(actionMap.get("createNewTestSuite")); // NOI18N
        menuFile.add(menuItemNewTestSuite);
        menuFile.add(menuSep1);

        menuItemExit.setAction(actionMap.get("quit")); // NOI18N
        menuFile.add(menuItemExit);

        menuBar.add(menuFile);

        menuHelp.setText("Help");

        menuItemAbout.setAction(actionMap.get("showAboutBox")); // NOI18N
        menuHelp.add(menuItemAbout);

        menuBar.add(menuHelp);

        statusPanel.setPreferredSize(new java.awt.Dimension(400, 22));

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(statusMessageLabel)
                            .addComponent(statusAnimationLabel))
                        .addGap(3, 3, 3))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel appPanel;
    private javax.swing.JButton buttonCreateTestSuite;
    private javax.swing.JPanel container;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelTestSuites;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuHelp;
    private javax.swing.JMenuItem menuItemAbout;
    private javax.swing.JMenuItem menuItemExit;
    private javax.swing.JMenuItem menuItemNewTestSuite;
    private javax.swing.JPopupMenu.Separator menuSep1;
    private javax.swing.JPanel panelContainerList;
    private javax.swing.JPanel panelContainerSelected;
    private javax.swing.JPanel panelSelectedTestSuite;
    private javax.swing.JPanel panelTestSuite;
    private javax.swing.JPanel panelTestSuiteList;
    private javax.swing.JPanel panelTestSuiteListContainer;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JPanel split;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;

    /**
     * @return the appPanel
     */
    public javax.swing.JPanel getAppPanel() {
        return appPanel;
    }

    /**
     * @param appPanel the appPanel to set
     */
    public void setAppPanel(javax.swing.JPanel appPanel) {
        this.appPanel = appPanel;
    }

    /**
     * @return the container
     */
    public javax.swing.JPanel getContainer() {
        return container;
    }

    /**
     * @param container the container to set
     */
    public void setContainer(javax.swing.JPanel container) {
        this.container = container;
    }

    /**
     * @return the buttonCreateTestSuite
     */
    public javax.swing.JButton getButtonCreateTestSuite() {
        return buttonCreateTestSuite;
    }

    /**
     * @param buttonCreateTestSuite the buttonCreateTestSuite to set
     */
    public void setButtonCreateTestSuite(javax.swing.JButton buttonCreateTestSuite) {
        this.buttonCreateTestSuite = buttonCreateTestSuite;
    }

    /**
     * @return the labelTestSuites
     */
    public javax.swing.JLabel getLabelTestSuites() {
        return labelTestSuites;
    }

    /**
     * @param labelTestSuites the labelTestSuites to set
     */
    public void setLabelTestSuites(javax.swing.JLabel labelTestSuites) {
        this.labelTestSuites = labelTestSuites;
    }

    /**
     * @return the mainPanel
     */
    public javax.swing.JPanel getMainPanel() {
        return mainPanel;
    }

    /**
     * @param mainPanel the mainPanel to set
     */
    public void setMainPanel(javax.swing.JPanel mainPanel) {
        this.mainPanel = mainPanel;
    }

    /**
     * @param menuBar the menuBar to set
     */
    public void setMenuBar(javax.swing.JMenuBar menuBar) {
        this.menuBar = menuBar;
    }

    /**
     * @return the menuFile
     */
    public javax.swing.JMenu getMenuFile() {
        return this.menuFile;
    }

    /**
     * @param menuFile the menuFile to set
     */
    public void setMenuFile(javax.swing.JMenu menuFile) {
        this.menuFile = menuFile;
    }

    /**
     * @return the menuItemExit
     */
    public javax.swing.JMenuItem getMenuItemExit() {
        return menuItemExit;
    }

    /**
     * @param menuItemExit the menuItemExit to set
     */
    public void setMenuItemExit(javax.swing.JMenuItem menuItemExit) {
        this.menuItemExit = menuItemExit;
    }

    /**
     * @return the menuItemNewTestSuite
     */
    public javax.swing.JMenuItem getMenuItemNewTestSuite() {
        return menuItemNewTestSuite;
    }

    /**
     * @param menuItemNewTestSuite the menuItemNewTestSuite to set
     */
    public void setMenuItemNewTestSuite(javax.swing.JMenuItem menuItemNewTestSuite) {
        this.menuItemNewTestSuite = menuItemNewTestSuite;
    }

    /**
     * @return the menuSep1
     */
    public javax.swing.JPopupMenu.Separator getMenuSep1() {
        return menuSep1;
    }

    /**
     * @param menuSep1 the menuSep1 to set
     */
    public void setMenuSep1(javax.swing.JPopupMenu.Separator menuSep1) {
        this.menuSep1 = menuSep1;
    }

    /**
     * @return the panelSelectedTestSuite
     */
    public javax.swing.JPanel getPanelSelectedTestSuite() {
        return panelSelectedTestSuite;
    }

    /**
     * @param panelSelectedTestSuite the panelSelectedTestSuite to set
     */
    public void setPanelSelectedTestSuite(javax.swing.JPanel panelSelectedTestSuite) {
        this.panelSelectedTestSuite = panelSelectedTestSuite;
    }

    /**
     * @return the panelTestSuite
     */
    public javax.swing.JPanel getPanelTestSuite() {
        return panelTestSuite;
    }

    /**
     * @param panelTestSuite the panelTestSuite to set
     */
    public void setPanelTestSuite(javax.swing.JPanel panelTestSuite) {
        this.panelTestSuite = panelTestSuite;
    }

    /**
     * @return the panelTestSuiteList
     */
    public javax.swing.JPanel getPanelTestSuiteList() {
        return panelTestSuiteList;
    }

    /**
     * @param panelTestSuiteList the panelTestSuiteList to set
     */
    public void setPanelTestSuiteList(javax.swing.JPanel panelTestSuiteList) {
        this.panelTestSuiteList = panelTestSuiteList;
    }

    /**
     * @return the progressBar
     */
    public javax.swing.JProgressBar getProgressBar() {
        return progressBar;
    }

    /**
     * @param progressBar the progressBar to set
     */
    public void setProgressBar(javax.swing.JProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    /**
     * @return the scrollPane
     */
    public javax.swing.JScrollPane getScrollPane() {
        return scrollPane;
    }

    /**
     * @param scrollPane the scrollPane to set
     */
    public void setScrollPane(javax.swing.JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    /**
     * @return the statusAnimationLabel
     */
    public javax.swing.JLabel getStatusAnimationLabel() {
        return statusAnimationLabel;
    }

    /**
     * @param statusAnimationLabel the statusAnimationLabel to set
     */
    public void setStatusAnimationLabel(javax.swing.JLabel statusAnimationLabel) {
        this.statusAnimationLabel = statusAnimationLabel;
    }

    /**
     * @return the statusMessageLabel
     */
    public javax.swing.JLabel getStatusMessageLabel() {
        return statusMessageLabel;
    }

    /**
     * @param statusMessageLabel the statusMessageLabel to set
     */
    public void setStatusMessageLabel(javax.swing.JLabel statusMessageLabel) {
        this.statusMessageLabel = statusMessageLabel;
    }

    /**
     * @return the statusPanel
     */
    public javax.swing.JPanel getStatusPanel() {
        return statusPanel;
    }

    /**
     * @param statusPanel the statusPanel to set
     */
    public void setStatusPanel(javax.swing.JPanel statusPanel) {
        this.statusPanel = statusPanel;
    }

    /**
     * @return the aboutBox
     */
    public JDialog getAboutBox() {
        return aboutBox;
    }

    /**
     * @param aboutBox the aboutBox to set
     */
    public void setAboutBox(JDialog aboutBox) {
        this.aboutBox = aboutBox;
    }

    /**
     * @return the panelTestSuiteListContainer
     */
    public javax.swing.JPanel getPanelTestSuiteListContainer() {
        return panelTestSuiteListContainer;
    }

    /**
     * @param panelTestSuiteListContainer the panelTestSuiteListContainer to set
     */
    public void setPanelTestSuiteListContainer(javax.swing.JPanel panelTestSuiteListContainer) {
        this.panelTestSuiteListContainer = panelTestSuiteListContainer;
    }

    /**
     * @return the panelContainerList
     */
    public javax.swing.JPanel getPanelContainerList() {
        return panelContainerList;
    }

    /**
     * @param panelContainerList the panelContainerList to set
     */
    public void setPanelContainerList(javax.swing.JPanel panelContainerList) {
        this.panelContainerList = panelContainerList;
    }

    /**
     * @return the panelContainerSelected
     */
    public javax.swing.JPanel getPanelContainerSelected() {
        return panelContainerSelected;
    }

    /**
     * @param panelContainerSelected the panelContainerSelected to set
     */
    public void setPanelContainerSelected(javax.swing.JPanel panelContainerSelected) {
        this.panelContainerSelected = panelContainerSelected;
    }
    
}
